<?php
include_once("../initial.php");
include_once("../driver/drive.php");
include_once("../driver/tableLocation.php");
include_once("../global/global_function.php");
class deviceWHandle
{
    private function insert_sql($array)
    {
        $tablename = tableLocation::deviceTable();
        $sql = "insert into $tablename(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "$key";
                $first=1;
            }
            else
            {
                $sql .= ",$key";
            }
        }
        $sql .=")values(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "'$value'";
                $first=1;
            }
            else
            {
                $sql .= ",'$value'";
            }
        }
        $sql .= ")";
        return $sql;
    }
    public function insert_data($array)
    {
        $sql = $this->insert_sql($array);
        return drive::exc($sql);
    }
    public function update_device($array, $id)
    {
        $sqlk=array();
        foreach($array as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(",",$sqlk);
        $table = tableLocation::deviceTable();
        $sql = "update $table set $sqls where id=$id";
        return drive::exc($sql);
    }
    public function delete_device($id)
    {
        $table = tableLocation::deviceTable();
        $sql = "delete from $table where id=$id";
        return drive::exc($sql);
        /*
        $array=array("check"=>"N");
        return $this->update_device($array,$id);
        */
    }
    public function insert_device_picture($array)
    {
    		$tablename = tableLocation::devicePictureTable();
        $sql = "insert into $tablename(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "$key";
                $first=1;
            }
            else
            {
                $sql .= ",$key";
            }
        }
        $sql .=")values(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "'$value'";
                $first=1;
            }
            else
            {
                $sql .= ",'$value'";
            }
        }
        $sql .= ")";
        return drive::exc($sql);
    }
    public function update_device_picture($array, $id)
    {
        $sqlk=array();
        foreach($array as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(",",$sqlk);
        $table = tableLocation::devicePictureTable();
        $sql = "update $table set $sqls where id=$id";
        return drive::exc($sql);
    }
    public function insert_device_course($array)
    {
    		$table = tableLocation::deviceCourseTable();
    		$sql = generateInsertSql($table, $array);
    		return drive::exc($sql);
    }
    public function remove_device_picture($pid)
    {
    		$table = tableLocation::devicePictureTable();
    		$sql = "delete from $table where id='$pid'";
    		return drive::exc($sql);
    }
    public function delete_device_course($condition)
    {
    		if($condition=='' || !is_array($condition) || count($condition)<=0)
    		{
    				return false;
    		}
    		$table = tableLocation::deviceCourseTable();
    		$sqlk=array();
        foreach($condition as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(" and ",$sqlk);
        $sql = "delete from $table where $sqls";
        return drive::exc($sql);
    }
    public function setDeviceUnavailable($did, $date, $remain)
    {
    		$remainTable = tableLocation::deviceRemainTable();
    		$sql = "insert into $remainTable(did, date, remain)values('$did', '$date', '$remain')";
    		return drive::exc($sql);
    }
    public function setDeviceAvailable($did, $date)
    {
    		$remainTable = tableLocation::deviceRemainTable();
    		$sql = "delete from $remainTable where did='$did' and date ='$date'";
    		return drive::exc($sql);
    }
}
?>